package com.gzdh.dishrecognition.ui.fragment.sysset;

import static androidx.fragment.app.FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.ui.fragment.FragmentAdapter;
import com.gzdh.dishrecognition.ui.holde.ButtonLayoutHolder;
import com.gzdh.dishrecognition.ui.holde.ButtonView;
import com.gzdh.dishrecognition.ui.view.MViewPager;

import java.util.ArrayList;
import java.util.List;

public class IdentifySetFragment extends Fragment {
    private Context mContext;
    private Activity mActivity;


    private ListAdapter listAdapter;

    private List<Fragment> fragmentlist;

    private ListView leftListView;
    private MViewPager fragmentViewPager2;
    private final int fragmentId = View.generateViewId();

    public IdentifySetFragment(Activity activity) {
        this.mActivity = activity;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        mContext = inflater.getContext();
        View view = getViews();
//        initDatas();
//        initDatasViewPager();
        return view;
    }

    private View getViews() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout mainLayout = new LinearLayout(mContext);
        mainLayout.setLayoutParams(layoutParams);
        mainLayout.setBackgroundColor(Color.WHITE);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        mainLayout.addView(getTitleView());   //标题初始化
        mainLayout.addView(getBody());        //主题内容
        return mainLayout;
    }


    private View getTitleView() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout titleLinear = new LinearLayout(mContext);
        titleLinear.setLayoutParams(layoutParams);
        titleLinear.setBackgroundColor(Color.WHITE);
        titleLinear.setOrientation(LinearLayout.VERTICAL);

        RelativeLayout titleRv = new RelativeLayout(mContext);
        titleRv.setLayoutParams(layoutParams);
        titleLinear.addView(titleRv);

        //-------------------------标题
        TextView titleTv = new TextView(mContext);
        titleTv.setText(" 菜 品 识 别 设 置 ");
        RelativeLayout.LayoutParams tt1 = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        int tt1Margins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, this.getResources().getDisplayMetrics());
        tt1.setMargins(tt1Margins, tt1Margins, tt1Margins, tt1Margins);
        tt1.addRule(RelativeLayout.CENTER_IN_PARENT);
        titleTv.setLayoutParams(tt1);
        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        titleRv.addView(titleTv);

        //----------------------------下划线
        View view = new View(mContext);
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        view.setBackgroundColor(Color.rgb(224, 224, 224));
        titleLinear.addView(view);

        return titleLinear;
    }

    @SuppressLint("ResourceType")
    private View getBody() {
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);


        leftListView = new ListView(mContext);
        leftListView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 5));
        linearLayout.addView(leftListView);

        fragmentViewPager2 = new MViewPager(mContext);
//        fragmentId = View.generateViewId();
        fragmentViewPager2.setId(fragmentId);
        fragmentViewPager2.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 3));
        linearLayout.addView(fragmentViewPager2);
        return linearLayout;
    }

    public void initDatas() {
        ArrayList<ButtonView> buttonListView = new ArrayList<>();
        buttonListView.add(new ButtonView("基础设备", R.mipmap.baseinfo_icon));
        buttonListView.add(new ButtonView("识别服务器", R.mipmap.baseinfo_icon));
        buttonListView.add(new ButtonView("系统设置", R.mipmap.baseinfo_icon));
        listAdapter = new ListAdapter(buttonListView);
        leftListView.setAdapter(listAdapter);
        leftListView.setDividerHeight(0);
        leftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listAdapter.setSelectedPosition(position);
                listAdapter.notifyDataSetInvalidated();
                switch (position) {
                    case 0:
                        fragmentViewPager2.setCurrentItem(0);
                        break;
                    case 1:
                        fragmentViewPager2.setCurrentItem(1);
                        break;
                    case 2:
                        fragmentViewPager2.setCurrentItem(2);
                        break;

                }

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        initDatas();
        initDatasViewPager();
    }

    public void initDatasViewPager() {
        fragmentlist = new ArrayList<Fragment>();
        fragmentlist.add(new IdentifyBaseSetFragment(mActivity));
        fragmentlist.add(new IdentifyServerSetFragment());
        fragmentlist.add(new IdentifySystemSetFragment());

        fragmentViewPager2.setScanScroll(false);
        fragmentViewPager2.setAdapter(new FragmentAdapter(getChildFragmentManager(), fragmentlist, mContext, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT));
    }


    public class ListAdapter extends BaseAdapter {

        ArrayList<ButtonView> arrayList = null;
        LayoutInflater inflater;
        View view;
        ButtonLayoutHolder buttonLayoutHolder;
        RelativeLayout buttonLayout = null;
        TextView buttonText = null;
        ImageView imagesView = null;


        private int selectedPosition = 0;// 选中的位置

        public ListAdapter(ArrayList<ButtonView> buttonListView) {
            // TODO Auto-generated constructor stub
            arrayList = buttonListView;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return arrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public void setSelectedPosition(int position) {
            selectedPosition = position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            inflater = LayoutInflater.from(mContext);
            view = inflater.inflate(R.layout.item_vh_layout, null, false);
            buttonLayoutHolder = (ButtonLayoutHolder) view.getTag();
            if (buttonLayoutHolder == null) {
                buttonLayoutHolder = new ButtonLayoutHolder();
                buttonLayoutHolder.buttonLayout = (RelativeLayout) view.findViewById(R.id.LinearLayoutButton);
                buttonLayoutHolder.textView = (TextView) view.findViewById(R.id.TextViewButton);
                buttonLayoutHolder.imagesView = (ImageView) view.findViewById(R.id.images);
                view.setTag(buttonLayoutHolder);
            }
            buttonLayout = buttonLayoutHolder.buttonLayout;
            buttonText = buttonLayoutHolder.textView;
            imagesView = buttonLayoutHolder.imagesView;
            if (selectedPosition == position) {
                buttonText.setSelected(true);
                buttonText.setPressed(true);
                buttonLayout.setBackgroundColor(Color.parseColor("#e4e8e9"));
                buttonText.setTextColor(Color.BLUE);
            } else {
                buttonText.setSelected(false);
                buttonText.setPressed(false);
                buttonLayout.setBackgroundColor(Color.parseColor("#002f4471"));
                buttonText.setTextColor(Color.BLACK);
            }
            imagesView.setImageResource(arrayList.get(position).imageId);
            buttonText.setText(arrayList.get(position).textViewId);
            return view;
        }
    }

}

